package com.longtop.framework.sys.file.excel;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.longtop.framework.common.Constant;
import com.longtop.framework.security.BaseUncheckedException;
import com.longtop.framework.sys.file.supports.UpLoadedFiles;
import com.longtop.framework.sys.file.supports.UploadFile;
import com.longtop.framework.util.StringUtil;

public class ExcelAnalyser {
	// private File excelFiles = null;
	/**
	 * 上传解析成功后删除
	 */
	protected boolean needDetete = false;

	/**
	 * 是否需要解析
	 */
	protected boolean needAnalysis = false;

	/**
	 * 从哪一行开始读取表头信息（从0行算起）：默认表头信息在第0行，若存在多行表头，则需要配置该属性，
	 */
	protected String readHeadfromRow = "0";

	protected String downloadTempFileName;

	/**
	 * 
	 * 是否需要检查上传excel文档与模板excel是否匹配
	 */
	private boolean needCheakUploadFile = false;

	/**
	 * 导入excel的模板excel
	 */
	protected String excelTempPath;

	/**
	 * excel内容映射的对象
	 */
	protected Object excelObject;

	public boolean isNeedDetete() {
		return needDetete;
	}

	public void setNeedDetete(boolean needDetete) {
		this.needDetete = needDetete;
	}

	public boolean isNeedAnalysis() {
		return needAnalysis;
	}

	public void setNeedAnalysis(boolean needAnalysis) {
		this.needAnalysis = needAnalysis;
	}

	public String getExcelTempPath() {
		return Constant.SERVERPATH + "\\" + excelTempPath;
	}

	public void setExcelTempPath(String excelTempPath) {
		this.excelTempPath = excelTempPath;
	}

	public Object getExcelObject() {
		return excelObject;
	}

	public void setExcelObject(Object excelObject) {
		this.excelObject = excelObject;
	}

	private File getExcelFile() {
		excelTempPath = StringUtil.replace(excelTempPath, "\"", "/");
		excelTempPath = StringUtil.replace(excelTempPath, "\\", "/");
		excelTempPath = StringUtil.replace(excelTempPath, "//", "/");
		excelTempPath = StringUtil.getString(excelTempPath);
		if (StringUtil.isEmpty(excelTempPath)) {
			throw new BaseUncheckedException("请检查配置文件 没有配置解析的excel模板路径。");
		}
		String serverPath = Constant.SERVERPATH;// 服务器绝对路径
		File excelFile = null;
		try {
			excelFile = new File(serverPath + excelTempPath);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BaseUncheckedException("请检查excel模板文件 没有找到excel模板。");
		}
		if (!excelFile.exists()) {
			throw new BaseUncheckedException("请检查excel模板文件 没有找到excel模板。");
		}

		return excelFile;
	}

	@SuppressWarnings("unchecked")
	private <T> List<T> getExcelInfo(File uploadExcelFile) throws BaseUncheckedException {
		if (needCheakUploadFile) {
			try {
				ReadExcelInfo.cheakExcelFile(getExcelFile(), uploadExcelFile, readHeadfromRow);
			} catch (Exception e1) {
				e1.printStackTrace();
				throw new BaseUncheckedException(e1.getMessage());
			}
		}
		List<T> list = null;
		Class clz = excelObject == null ? null : excelObject.getClass();
		try {
			list = ReadExcelInfo.readExcelData(uploadExcelFile, clz, readHeadfromRow);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BaseUncheckedException(e.getMessage());
		}
		return list;
	}

	private void deleteExcelFile(UploadFile ups) {
		if (needDetete) {
			ups.setTempFile(true);
		} else {
			ups.setTempFile(false);
		}
	}

	private <T> List<T> parseUpLoadExcel(File uploadExcelFile) throws BaseUncheckedException {
		List<T> listReturn = null;
		if (!needAnalysis) {
			return null;
		} else {
			listReturn = getExcelInfo(uploadExcelFile);
			return listReturn;
		}
	}

	/**
	 * 解析上传EXCEL重要方法
	 * 
	 * @param upLoad
	 * @return
	 * @throws BaseUncheckedException
	 */
	@SuppressWarnings("unchecked")
	public UpLoadedFiles parseUpLoadFiles(UpLoadedFiles upLoad) throws BaseUncheckedException {
		String deleteExit = StringUtil.getString(upLoad.getMyUpload().getParameter("deleteExit"));
		if (deleteExit.equals("true")) {
			upLoad.setDeleteExit(Boolean.valueOf(deleteExit));
		}

		List excelObject = new ArrayList();
		List<UploadFile> upFiles = upLoad.getUpLoadFiles();
		for (UploadFile ups : upFiles) {
			// 组装解析EXCEL的数据到LIST
			excelObject.addAll(parseUpLoadExcel(ups.getFileObject()));
			if (upLoad.isDeleteExit()) {
				ups.setTempFile(true);
			} else {
				deleteExcelFile(ups);
			}
		}
		if (excelObject.size() > 0) {
			// 设置组装好的数据LIST。最后用于业务层取LIST。
			upLoad.setExcelObjectList(excelObject);
		}

		return upLoad;
	}

	public boolean isNeedCheakUploadFile() {
		return needCheakUploadFile;
	}

	public void setNeedCheakUploadFile(boolean needCheakUploadFile) {
		this.needCheakUploadFile = needCheakUploadFile;
	}

	public String getDownloadTempFileName() {
		return downloadTempFileName;
	}

	public void setDownloadTempFileName(String downloadTempFileName) {
		this.downloadTempFileName = downloadTempFileName;
	}

	public String getReadHeadfromRow() {
		return readHeadfromRow;
	}

	public void setReadHeadfromRow(String readHeadfromRow) {
		this.readHeadfromRow = readHeadfromRow;
	}

}
